<script lang="ts" setup>
import type { ExtendedModalApi } from '@vben/common-ui';

import { onMounted, ref } from 'vue';

import { useFs } from '@fast-crud/fast-crud';

import { getValidateRulesByFs } from '#/api/common/validateByFs';
import { DefUserConfig } from '#/api/devOperation/tenant/defUser';
import { ActionEnum } from '#/enums/commonEnum';

import { createCrudOptions, frontRules } from './data/crud';
import RestPasswordModal from './modules/reset-password.vue';

defineOptions({
  name: '用户管理',
  inheritAttrs: false,
});
const restPasswordRef = ref<ExtendedModalApi>();
const { crudRef, crudBinding, crudExpose, appendCrudOptions } = useFs({
  createCrudOptions,
  context: { restPasswordRef },
});

// 页面打开后获取列表数据
onMounted(async () => {
  const addFormOptions = await getValidateRulesByFs({
    Api: DefUserConfig.Save,
    mode: ActionEnum.ADD,
    customRules: frontRules(crudExpose, ActionEnum.ADD),
    trigger: ['blur', 'change'],
  });
  const editFormOptions = await getValidateRulesByFs({
    Api: DefUserConfig.Update,
    mode: ActionEnum.EDIT,
    customRules: frontRules(crudExpose, ActionEnum.EDIT),
    trigger: ['blur', 'change'],
  });
  appendCrudOptions({ ...addFormOptions, ...editFormOptions });
  crudExpose.doRefresh();
});
function handleSuccess() {
  crudExpose.doRefresh();
}
</script>

<template>
  <FsPage>
    <FsCrud ref="crudRef" v-bind="crudBinding" />
    <RestPasswordModal ref="restPasswordRef" @success="handleSuccess" />
  </FsPage>
</template>
